package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Browse;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.Travelcontent;
import cn.wolfcode.trip.base.mapper.BrowseMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.TravelcontentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class TravelServiceImpl implements ITravelService {

    //注入2个mapper接口对象
    @Autowired
    private TravelMapper travelMapper ;
    @Autowired
    private TravelcontentMapper travelcontentMapper;

    @Autowired
    private BrowseMapper browseMapper ;


    public PageInfo<Travel> query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize(), queryObject.getOrderBy());
        List<Travel> list = travelMapper.selectForList(queryObject);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(Travel travel) {

        if (travel.getId() == null) {
            //在新增这边设置创建时间和最后更新时间
            travel.setCreateTime(new Date());//创建的时间
            travel.setLastUpdateTime(new Date());//最后更新的时间
            //该要手动设置的都可以了，调用mapper接口的方法
            travelMapper.insert(travel);
            //新增完游记之后还要将游记中取出游记内容这个对象去更新数据库中的数据
            Travelcontent travelcontent = travel.getTravelcontent();
            //因为是主键关联，一对一，所以将游记的Id给到游记文本的ID
            travelcontent.setId(travel.getId());
            //新增游记文本
            travelcontentMapper.insert(travelcontent);
        }else{
            travel.setLastUpdateTime(new Date());//最后更新的时间
            //设置完修改时间之后再进行修改
            travelMapper.updateByPrimaryKey(travel);
            //修改完游记之后还要将游记中取出游记内容这个对象去更新数据库中的数据
            Travelcontent travelcontent = travel.getTravelcontent();
            //因为是主键关联，一对一，所以将游记的Id给到游记文本的ID
            travelcontent.setId(travel.getId());
            //新增游记文本
            travelcontentMapper.updateByPrimaryKey(travelcontent);
        }
    }

    public Travel get(Long id) {
        //通过id去查到指定的游记对象
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public Travelcontent getContent(@Param("id") Long id) {
        return travelcontentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeState(Long id, Integer state) {
        //还需要判断是拒绝操作还是发布操作
        if(state==Travel.STATE_RELEASE){
            //是发布操作的话需要设置发布时间
            travelMapper.changeState(id,state,new Date());
        }else if(state==Travel.STATE_REFUSE){
            travelMapper.changeState(id,state,null);
        }
    }

    @Override
    public Browse getBrowse(Long id) {
        return browseMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdateBrowse(Travel travel) {
        Browse browses = browseMapper.selectByPrimaryKey(travel.getId());
        if (browses == null) {
            Browse bro = new Browse();
            //因为是主键关联,所以把攻略的id设置给浏览记录的id
            bro.setId(travel.getId());
            //新增浏览次数
            bro.setTotal(1);
            browseMapper.insert(bro);
        }else{
            //获取当前的浏览量
            Integer total = browses.getTotal();
            //修改浏览次数
            browses.setTotal(total + 1);
            browseMapper.updateByPrimaryKey(browses);
        }
    }

    @Override
    public List<Travel> queryForBrowse(Long id) {
        return travelMapper.selectForBrowse(id);
    }
}
